package cn.wolfcode.shop.server.service.impl;


import cn.wolfcode.shop.api.domain.Brand;
import cn.wolfcode.shop.api.page.PageResult;
import cn.wolfcode.shop.api.query.BrandQueryObject;
import cn.wolfcode.shop.api.service.IBrandService;
import cn.wolfcode.shop.server.mapper.BrandMapper;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements IBrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Override
    public int save(Brand record) {
        return brandMapper.insert(record);
    }

    @Override
    public int update(Brand record) {
        return brandMapper.updateByPrimaryKey(record);
    }

    @Override
    public Brand get(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Brand> getAll() {
        return brandMapper.selectAll();
    }


    @Override
    public PageResult query(BrandQueryObject qo) {
        int totalCount = brandMapper.queryTotal(qo);
        if (totalCount == 0) {
            return PageResult.empty(qo.getCurrentPage(), qo.getPageSize());
        } else {
            List<Brand> list = brandMapper.queryForList(qo);
            return new PageResult(totalCount, list, qo.getCurrentPage(), qo.getPageSize());
        }
    }


    @Override
    public int deleteBrand(Long id) {
        Brand brand = this.get(id);
        brand.setMods(0L);  //设置为不显示
        this.update(brand);
        return this.update(brand);
    }

    @Override
    public int recoveryBrand(Long id) {
        Brand brand = this.get(id);
        brand.setMods(1L);  //设置为显示
        this.update(brand);
        return this.update(brand);
    }

    //编辑上传rul保存
    @Override
    public void saveLogoUrl(String logoURL, Long id) {
        System.out.println("----------logoURL--------------" + logoURL);
        if (id != null) {
            Brand currentBrand = this.get(id);
            currentBrand.setLogo(logoURL);
            brandMapper.updateByPrimaryKey(currentBrand);
        }
    }

    //新增上传
    @Override
    public void saveNewLogoUrl(String logoURL) {
        System.out.println("----------logoURL--------------" + logoURL);
            Brand newBrand = new Brand();
            newBrand.setLogo(logoURL);
            //brandMapper.insert(newBrand);
    }


    //编辑保存
    @Override
    public void editBrandSave(Brand brand) {
        System.out.println("-----------brand3333----------" + brand);
        Brand currentBrand = this.get(brand.getId());
        System.out.println("-----------brand.getId()----------" + brand.getId());
        currentBrand.setChineseName(brand.getChineseName());
        currentBrand.setEnglishName(brand.getEnglishName());
        currentBrand.setSequence(brand.getSequence());
        currentBrand.setDescription(brand.getDescription());
        currentBrand.setUrl(brand.getUrl());
        currentBrand.setLastModifiedDate(new Date());  //编辑时间更新
        currentBrand.setBrandCatalogs(brand.getBrandCatalogs()); // 品牌分类
        currentBrand.setUnderProducts(brand.getUnderProducts());  //旗下商品

        this.update(currentBrand);
    }


    //新增保存
    @Override
    public void addBrandSave(Brand brand) {
        System.out.println("-----------brand111----------" + brand);
        Brand newBrand = new Brand();
        newBrand.setChineseName(brand.getChineseName());
        newBrand.setEnglishName(brand.getEnglishName());
        newBrand.setSequence(brand.getSequence());
        newBrand.setDescription(brand.getDescription());
        newBrand.setUrl(brand.getUrl());
        newBrand.setCreatedDate(new Date());  //编辑时间插入
        newBrand.setMods(1L);
        newBrand.setBrandCatalogs(brand.getBrandCatalogs()); // 品牌分类
        newBrand.setUnderProducts(brand.getUnderProducts());  //旗下商品
        newBrand.setLogo(brand.getLogo());

        this.save(newBrand);
    }


}
